package com.joca.scheduling.pages;

import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;

import com.joca.scheduling.database.ScheduleData;
import com.joca.scheduling.database.UserData;
import com.joca.scheduling.entities.Role;
import com.joca.scheduling.entities.Schedule;
import com.joca.scheduling.entities.User;
import com.joca.scheduling.hibernateservices.UserServiceHibernate;

public class Request {

	@SessionState(create = false)
	@Property
	private User user;
	private boolean userExists;
	@Property
	private Schedule schedule;
	@Persist
	private User professor;

	public User getProfessor() {
		return professor;
	}

	public void setProfessor(User professor) {
		this.professor = professor;
	}

	Object onActivate(int userID) {
		// ID = 4;
		professor = UserServiceHibernate.getUserServiceHibernate().getUserByID(
				userID);// UserData.getInstance().getUserByID(userID);
		if (!userExists)
			return Index.class;
		else if (user.getRole() == Role.Student) {
			return null;
		}
		return Index.class;
	}

	// @Persist
	// private int ID;

	Object onSuccess() {
		// schedule.setScheduleID(ID);
		// ID++;
		schedule.setReason("");
		schedule.setScheduleRequest(user.getUserID());
		schedule.setScheduleRespond(professor.getUserID());
		// schedule.setScheduleRequest(user);
		// schedule.setScheduleRespond(professor);
		UserServiceHibernate.getUserServiceHibernate().addSchedule(schedule);
		// ScheduleData.getInstance().addSchedule(schedule);
		return null;
	}

}
